<template>
  <div>
    <crumbs :BreadCrumb="BreadCrumb"></crumbs>
    <div>
      <div class="top_inp">
        <el-input @input="bindinput" v-model="roleName" placeholder="请输入查询内容"></el-input>
      </div>
      <!-- <el-button class="top_but" icon="el-icon-search"  type="primary"></el-button> -->
    </div>
    <div class="top_buts">
      <el-button class="" type="primary" @click="binddialogFormVisible(true)">添加</el-button>
      <el-button class="top_but" type="danger">删除</el-button>
    </div>
    <!-- 列表 -->
    <div>

      <el-table
          ref="multipleTable"
          :data="list"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            label="序号"
            type="index"
            width="120">
        </el-table-column>
        <el-table-column
            prop="roleName"
            label="角色名称"
            width="220">
        </el-table-column>
        <el-table-column
            prop="remarks"
            label="备注"
            show-overflow-tooltip>
        </el-table-column>
        <el-table-column width="200" prop="address" label="操作">
          <template slot-scope="scope">
            <el-button @click="getqueryRolePermissions(scope.row)" type="primary" size="small">编辑</el-button>
            <el-button @click="deleteRole(scope.row)" type="danger" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!-- 弹框 -->
    <el-dialog :title="id==null?'添加角色':'编辑角色'" :visible.sync="dialogFormVisible" width="600px">
      <div class="dialog">
        <el-form :model="form" ref="ruleForm" :rules="rules">
          <el-form-item label="角色名称" :label-width="formLabelWidth" prop="roleName">
            <el-input v-model="form.roleName" @input="forceupdata" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remarks" :label-width="formLabelWidth">
            <el-input type="textarea" @input="forceupdata" :autosize="{ minRows: 4, maxRows: 6}" v-model="form.remarks"
                      autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="菜单设置" prop="menu" :label-width="formLabelWidth">
            <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
              <el-checkbox v-for="(city,index) in cities" :label="city.menuId" :key="index">{{ city.menuCaption }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="权限设置" prop="role" :label-width="formLabelWidth">
            <el-checkbox-group v-model="checkedCitiess" @change="handleCheckedCitiesChange">
              <el-checkbox v-for="(city,index) in citiess" :label="city.permission" :key="index">
                {{ city.permissionName }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addinsertSysRole('ruleForm')">确 定</el-button>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'

import crumbs from "../../views/Crumbs.vue";

export default {
  components: {
    crumbs,
  },
  data() {
    return {
      formLabelWidth: '80px',
      form: {
        roleName: '',
        remarks: ''
      },
      rules: {
        roleName: [
          {required: true, message: '请输入角色名称', trigger: 'blur'}
        ],
        remarks: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
        // menu: [
        //   { type: 'array', required: true, message: '请至少选择一个权限', trigger: 'change' }
        // ],
        // role: [
        //   { type: 'array', required: true, message: '请至少选择一个权限', trigger: 'change' }
        // ],
      },
      topDeptList: [],
      dialogFormVisible: false,
      BreadCrumb: [
        {path: "", name: "系统权限"},
        {path: "", name: "权限管理"},
      ],

      value: '123',
      clearable: false,
      roleName: '',
      list: [],
      pageSize: 10,
      pageNo: 1,
      checkedCities: [],
      checkedCitiess: [],
      cities: [],
      citiess: [],
      id: null
    }
  },
  created() {
    this.getquerySysRoleList()
    this.binddialogFormVisible(false)
  },
  methods: {
    forceupdata() {
      this.$forceUpdate()
    },
    // 权限编辑查询
    getqueryRolePermissions(row) {
      this.dialogFormVisible = true
      this.$axios.getqueryRolePermissions({
        roleId: row.roleId
      }).then(res => {
        if (res.code == '00000') {
          this.form.roleName = res.data.roleName
          this.form.remarks = res.data.remarks
          this.id = res.data.roleId
          let arr = []
          for (let i = 0; i < res.data.menu.length; i++) {
            arr.push(res.data.menu[i].menuId)
          }
          this.checkedCities = arr
          let arr1 = []
          for (let i = 0; i < res.data.role.length; i++) {
            arr1.push(res.data.role[i].permission)
          }
          this.checkedCitiess = arr1
        }
        console.log(row)
      })
    },

    // 添加权限保存按钮
    addinsertSysRole(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.id) {
            this.$axios.selupdateSysRole({
              roleName: this.form.roleName,
              remarks: this.form.remarks,
              menu: this.checkedCities,
              role: this.checkedCitiess,
              roleId: this.id
            }).then(res => {
              if (res.code == '00000') {
                this.$message({
                  type: 'success',
                  message: res.msg
                })
                this.dialogFormVisible = false
                this.getquerySysRoleList()
              }
            })
          } else {

            this.$axios.addinsertSysRole({
              roleName: this.form.roleName,
              remarks: this.form.remarks,
              menu: this.checkedCities,
              role: this.checkedCitiess
            }).then(res => {
              if (res.code == '00000') {
                this.$message({
                  type: 'success',
                  message: res.msg
                })
                this.dialogFormVisible = false
                this.getquerySysRoleList()
              }
              console.log(res)
            })
          }
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 权限选择事件
    handleCheckedCitiesChange(row) {
      console.log(row)
    },
    // 添加权限
    binddialogFormVisible(row) {
      this.dialogFormVisible = row
      this.id = null
      this.form = {
        roleName: '',
        remarks: ''
      },

          this.checkedCities = [],
          this.checkedCitiess = [],
          this.getquerySysMenu()
    },
    // 权限请求
    getquerySysMenu() {
      this.$axios.getquerySysMenu({}).then(res => {
        if (res.code == '00000') {
          let cities = res.data.menu
          this.cities = cities

          console.log(this.cities)
          this.citiess = res.data.role
        }
        console.log(res)
      })
    },
    // 列表删除
    deleteRole(row) {
      this.$confirm('此操作将删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.deleteRole({
          roleId: row.roleId
        }).then(res => {
          console.log(res)
          if (res.code == '00000') {
            this.$message({
              message: res.msg,
              type: 'success'
            });
            this.getquerySysRoleList()
          } else {
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 表单输入
    bindinput() {
      console.log('qw3')
      this.getquerySysRoleList()
    },
    // 权限列表查询
    getquerySysRoleList() {
      this.$axios.getquerySysRoleList({
        roleName: this.roleName,
        pageSize: this.pageSize,
        pageNo: this.pageNo,
      }).then(res => {
        if (res.code == '00000') {
          this.list = res.data.records
          this.total = res.data.total
        }
        console.log(res)
      })
    },
    // 每页几条
    handleSizeChange(val) {
      this.pageSize = val
      this.getquerySysRoleList()
    },
    // 第几页
    handleCurrentChange(val) {
      this.pageNo = val
      this.getquerySysRoleList()
    }
  }
}
</script>

<style scoped>
.top_but {
  margin-left: 20px;
}

.top_buts {
  margin-top: 10px;
  margin-bottom: 10px;
}

.pagination {
  text-align: center;
}

.top_inp {
  width: 300px;
}

.dialog {
  width: 500px;
  margin: 0px auto;
}
</style>
